package com.wenx.mutiwebservices;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;
import org.apache.commons.lang.StringEscapeUtils;

import java.net.URLEncoder;

public class JettyToAxis2Web {

    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRouteDefinition(initRoute2());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    public static RouteDefinition initRoute2(){
        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition
                .from("jetty:http://localhost:9080/camel")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("访问成功!");
                        //exchange.getOut().setHeader(Exchange.HTTP_METHOD,"GET");
                        exchange.getOut().setHeader(Exchange.HTTP_QUERY,"id=111&name=aaa");
                    }
                })
                .to("http://localhost:8087/services/axis2WebService/sayHello")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        String param = "data={\"msg\":\"成功发送数据\",\"incValue\":789,\"data\":[{\"name\":\"张三\",\"id\":789,\"time\":\"2018-04-02 17:19:23\"},{\"name\":\"张三\",\"id\":789,\"time\":\"2018-04-02 17:19:23\"},{\"name\":\"张三\",\"id\":789,\"time\":\"2018-04-02 17:19:23\"},{\"name\":\"张三\",\"id\":789,\"time\":\"2018-04-02 17:19:23\"},{\"name\":\"张三\",\"id\":789,\"time\":\"2018-04-02 17:19:23\"}],\"incField\":\"id\",\"totalCount\":500,\"status\":0}";
                        //param = StringEscapeUtils.escapeHtml(param);
                        param = URLEncoder.encode(param,"utf-8");
                        exchange.getOut().setHeader(Exchange.HTTP_QUERY,param);
                    }
                })
                .to("http://localhost:8087/services/axis2WebService/welcome")
//                .to("cxf:"
//                        + "http://localhost:8087/services/axis2WebService/sayHello" //service address
//                        + "?"
//                        + "wsdlURL=http://localhost:8087/services/axis2WebService?wsdl"    //wsdl url
//                        + "&"
//                        + "dataFormat=MESSAGE"
//                                +         //dataformat type
//                        "&" +
//                        "endpointName=axis2WebServiceSOAP11port_http"
////                        "&" +
////                        "serviceName=WeatherWebService"
//                )
//                .process(new Processor() {
//                    @Override
//                    public void process(Exchange exchange) throws Exception {
//                        //强转成String可能会出现问题，要注意
//                        String body = exchange.getIn().getBody(String.class);
//                        System.out.println("body = " + body);
//                    }
//                })
                ;
        return routeDefinition;
    }
}
